﻿namespace HelperLibrary;

/// <summary>
/// 任务管理器
/// </summary>
public class TaskManager
{
    /// <summary>
    /// 锁
    /// </summary>
    private static object _lock = new object();
    /// <summary>
    /// 锁
    /// </summary>
    private static object _lockImport = new object();
    /// <summary>
    /// 任务字典
    /// </summary>
    private static Dictionary<string, TaskInfo> TaskDic = new Dictionary<string, TaskInfo>();
    /// <summary>
    /// 导出任务字典
    /// </summary>
    private static Dictionary<string, TaskInfo> TaskDicImport = new Dictionary<string, TaskInfo>();

    /// <summary>
    /// 查询任务是否正在处理
    /// </summary>
    /// <param name="taskKey"></param>
    /// <returns></returns>
    public static bool IsProcessing(string taskKey)
    {
        return TaskDic.ContainsKey(taskKey);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static object RetuenDic()
    {
        var sumInspectionCount = 0;
        var sumCampusList = new List<int>();
        if (TaskDic != null && TaskDic.Count > 0)
        {
            foreach (var item in TaskDic)
            {
                if (item.Value != null)
                {
                    sumInspectionCount += item.Value.InspectionCount;
                    if (item.Value.CampusIds != null && item.Value.CampusIds.Any())
                    {
                        sumCampusList.AddRange(item.Value.CampusIds);
                    }
                }
            }
            var sumCampusCount = 0;
            if (sumCampusList.Any())
            {
                sumCampusCount = sumCampusList.Distinct().Count();
            }
            var obj = new { sumCampusCount, sumInspectionCount, TaskDic };
            return obj;
        }
        return new object();
    }

    /// <summary>
    /// 获取任务进度
    /// </summary>
    /// <param name="taskKey"></param>
    /// <returns></returns>
    public static int GetProgress(string taskKey)
    {
        lock (_lock)
        {
            if (TaskDic.ContainsKey(taskKey))
            {
                int progress = TaskDic[taskKey].Progress;
                if (progress > 100)
                {
                    progress = 100;
                }
                return progress;
            }
        }
        return 100;
    }

    /// <summary>
    /// 添加任务
    /// </summary>
    /// <param name="taskKey"></param>
    public static void AddTask(string taskKey, TaskInfo taskInfo)
    {
        lock (_lock)
        {
            if (TaskDic.ContainsKey(taskKey))
            {
                TaskDic[taskKey] = taskInfo;
            }
            else
            {
                TaskDic.Add(taskKey, taskInfo);
            }
        }
    }
   
    /// <summary>
    /// 是否存在任务
    /// </summary>
    /// <returns></returns>
    public static bool IsExist()
    {
        if (TaskDic.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 删除任务
    /// </summary>
    /// <param name="taskKey"></param>
    public static void Remove(string taskKey)
    {
        TaskDic.Remove(taskKey);
        TaskDicImport.Remove(taskKey);
    }

    /// <summary>
    /// 清除任务
    /// </summary>
    /// <param name="taskKey"></param>
    public static void Clear()
    {
        TaskDic.Clear();
    }

    #region 导入任务

    /// <summary>
    /// 添加导入任务
    /// </summary>
    /// <param name="taskKey"></param>
    /// <param name="taskInfo"></param>
    public static void AddTaskImport(string taskKey, TaskInfo taskInfo)
    {
        lock (_lockImport)
        {
            if (TaskDicImport.ContainsKey(taskKey))
            {
                TaskDicImport[taskKey] = taskInfo;
            }
            else
            {
                TaskDicImport.Add(taskKey, taskInfo);
            }
        }
    }
    /// <summary>
    /// 获取导入任务进度
    /// </summary>
    /// <param name="taskKey"></param>
    /// <returns></returns>
    public static int GetProgressImport(string taskKey)
    {
        lock (_lockImport)
        {
            if (TaskDicImport.ContainsKey(taskKey))
            {
                int progress = TaskDicImport[taskKey].Progress;
                if (progress > 100)
                {
                    progress = 100;
                }
                return progress;
            }
        }
        return 100;
    }
    /// <summary>
    /// 查询导入任务是否正在处理
    /// </summary>
    /// <param name="taskKey"></param>
    /// <returns></returns>
    public static bool IsProcessingImport(string taskKey)
    {
        return TaskDicImport.ContainsKey(taskKey);
    }
    /// <summary>
    /// 删除导入任务
    /// </summary>
    /// <param name="taskKey"></param>
    public static void RemoveImport(string taskKey)
    {
        TaskDicImport.Remove(taskKey);
    }
    #endregion
}
/// <summary>
/// 任务信息
/// </summary>
public class TaskInfo
{
    /// <summary>
    /// 进度
    /// </summary>
    public int Progress { get; set; }
    /// <summary>
    /// 总数
    /// </summary>
    public int Count { get; set; }
    /// <summary>
    /// 异常数
    /// </summary>
    public int EXCount { get; set; }
    /// <summary>
    /// 巡察数
    /// </summary>
    public int InspectionCount { get; set; }
    /// <summary>
    /// 学校id集合
    /// </summary>
    public List<int> SchoolIds { get; set; }
    /// <summary>
    /// 实验室id集合
    /// </summary>
    public List<int> CampusIds { get; set; }
    /// <summary>
    /// 生态id集合
    /// </summary>
    public List<int> EcologyIds { get; set; }
}
